﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;
using Mimoza.ObjectLayer;
using Mimoza.PluginLayer;
using Mimoza.Apps.Console.Resources;

namespace Mimoza.Apps.Console.Commands
{
    public partial class PolicyWizardPluginTypePage : Mimoza.GUI.WizardPage
    {       
        public PolicyWizardPluginTypePage()
        {
            InitializeComponent();
        }
        override public void Init()
        {
            this.IsConfigured = false;
            if (null != this.PolicyWizard.Plugin)
            {
                this.IsConfigured = false;
            }

            FillTable();
            this.IsConfigured = false;
        }
        private PolicyWizard PolicyWizard
        {
            get
            {
                return (this.Wizard as PolicyWizard);
            }
        }
        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                this.Wizard.DeletePagesFrom(2);
                return true;
            }
            else if (direction == WizardPageLeavingDirection.Forward)
            {
                if (null != this.PolicyWizard.Plugin)
                {
                    this.IsConfigured = true;
                    return true;
                }

                return false;
            } 

            return true;
        }
        void FillTable()
        {
            int imageIndex = 1;
            ImageList smallIcons = new ImageList();
            ImageList largeIcons = new ImageList();
            
            smallIcons.ImageSize = new Size(16, 16);
            largeIcons.ImageSize = new Size(32, 32);

            smallIcons.Images.Add(Resource.DefaultPolicyIcon);
            largeIcons.Images.Add(Resource.DefaultPolicyIcon);

            this.listViewPolicy.Items.Clear();
            this.listViewPolicy.SmallImageList = smallIcons;
            this.listViewPolicy.LargeImageList = largeIcons;

            foreach (IConfigPlugin plugin in this.PolicyWizard.PluginManager.Plugins)
            {
                ListViewItem li = new ListViewItem(plugin.Name);

                if (null != plugin.Icon)
                {
                    smallIcons.Images.Add(new Icon(plugin.Icon, smallIcons.ImageSize));
                    largeIcons.Images.Add(new Icon(plugin.Icon, largeIcons.ImageSize));

                    li.ImageIndex = imageIndex;
                    imageIndex++;
                }
                else 
                {
                    li.ImageIndex = 0;
                }

                li.Tag = plugin;
                li.SubItems.Add(plugin.Description);

                if (plugin.IsHardware)
                {
                    li.Group = listViewPolicy.Groups[0];
                }
                else
                {
                    li.Group = listViewPolicy.Groups[1];
                }

                this.listViewPolicy.Items.Add(li);
            }
        }
        void AddPluginPages()
        {
            this.Wizard.DeletePagesFrom(2);

            if (null == this.PolicyWizard.Plugin)
            {
                return;
            }

            foreach (Mimoza.GUI.IWizardPage page in this.PolicyWizard.Plugin.WizardPages)
            {
                this.Wizard.AddPage(page);
            }
        }
        private void listViewPolicy_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listViewPolicy.SelectedItems.Count != 0)
            {
                if (this.PolicyWizard != null)
                {
                    this.PolicyWizard.Plugin = listViewPolicy.SelectedItems[0].Tag as IConfigPlugin;
                    this.PolicyWizard.UserData = this.PolicyWizard.Plugin.Deserialize(null);
                }
            }
            else
            {
                this.PolicyWizard.Plugin = null;
            }

            AddPluginPages();
            this.RaiseOnPageModified();
        }
    }
}
